package com.azt.data.action;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.azt.data.permission.MenuPermission;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.azt.api.model.Pagination;
import com.azt.api.service.PaginationService;
import com.azt.data.utils.CommonUtil;
import com.azt.data.utils.ExcelExportEntityUtil;
import com.azt.data.utils.Globals;
import com.azt.data.utils.RequestUtils;
import com.azt.provider.mapper.ShopMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaoleilu.hutool.util.StrUtil;

import net.sf.json.JSONArray;

/** 
 * @ClassName: ShopAction 
 * @Description: 商铺
 * @author: zhaheng
 * @date: 2017年4月27日 下午4:00:32  
 */
@Controller
@RequestMapping(value = "/shop")
public class ShopAction {
	
	@Autowired
	private ShopMapper shopMapper;
	
	@Autowired
	private PaginationService paginationService;
	
	/**店铺概览开始*/
	@MenuPermission
	@RequestMapping(value = "/shopview")
	public String shopview(ModelMap model){
		
		//当前平台交易过的店铺总数
		int tradeShopCount=shopMapper.getTradeShopCount();
		//当前平台店铺总数
		int shopCount = shopMapper.getShopCount();
		//商城交易总金额
		String shopTotal= new BigDecimal(shopMapper.getShopTotal()).divide(new BigDecimal("10000")).setScale( 2 , BigDecimal.ROUND_HALF_UP).toString();
		//当前平台商品总数
		int goodsCount = shopMapper.getGoodsCount();
		
		model.addAttribute("tradeShopCount", tradeShopCount);
		model.addAttribute("shopCount", shopCount);
		model.addAttribute("shopTotal", shopTotal);
		model.addAttribute("goodsCount", goodsCount);
		return "shop/shopview";
	}
	
	/** 
	 * @Title: getProductTotalRate 
	 * @Description: 每月自营商品订单金额及增长率 
	 * @param startDate
	 * @param endDate
	 * @return
	 * @return: JSONObject
	 * @author: 查恒 2017年4月28日 下午3:42:53 
	 */
	@ResponseBody
	@RequestMapping(value = "/getProductTotalRate")
	public JSONObject getProductTotalRate(String startDate, String endDate) {
		JSONObject json = new JSONObject();
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		if (StrUtil.isBlank(startDate)) {
			endDate = sdf.format(c.getTime());
			c.add(Calendar.MONTH, -11);
			startDate = sdf.format(c.getTime());
		}
		List<Map<String, Object>> productTotalRateList = shopMapper.getProductTotalRate(startDate, endDate);
		String jsonString = JSON.toJSONString(productTotalRateList, CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		json.put("startDate", startDate);
		json.put("endDate", endDate);
		return json;
	}
	
	
	/** 
	 * @Title: getShopTotalRate 
	 * @Description: 每月商品订单金额及增长率
	 * @return
	 * @return: JSONObject
	 * @author: 查恒 2017年4月28日 下午3:45:40 
	 */
	@ResponseBody
	@RequestMapping(value = "/getShopTotalRate")
	public JSONObject getShopTotalRate(String startDate, String endDate){
		JSONObject json = new JSONObject();
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		if (StrUtil.isBlank(startDate)) {
			endDate = sdf.format(c.getTime());
			c.add(Calendar.MONTH, -11);
			startDate = sdf.format(c.getTime());
		}
		List<Map<String, Object>> shopTotalRateList = shopMapper.getShopTotalRate(startDate, endDate);
		String jsonString = JSON.toJSONString(shopTotalRateList, CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		json.put("startDate", startDate);
		json.put("endDate", endDate);
		return json;
	}
	
	/** 
	 * @Title: getProductCountRate 
	 * @Description: 每月发布商品数量及增长率 
	 * @param startDate
	 * @param endDate
	 * @return
	 * @return: JSONObject
	 * @author: 查恒 2017年4月28日 下午3:49:48 
	 */
	@ResponseBody
	@RequestMapping(value = "/getProductCountRate")
	public JSONObject getProductCountRate(String startDate, String endDate){
		JSONObject json = new JSONObject();
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		if (StrUtil.isBlank(startDate)) {
			endDate = sdf.format(c.getTime());
			c.add(Calendar.MONTH, -11);
			startDate = sdf.format(c.getTime());
		}
		List<Map<String, Object>> productCountRateList = shopMapper.getProductCountRate(startDate, endDate);
		String jsonString = JSON.toJSONString(productCountRateList, CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		json.put("startDate", startDate);
		json.put("endDate", endDate);
		return json;
	}
	
	
	

	/** 
	 * @Title: getProductCountChart 
	 * @Description: 各品类商品数量占比
	 * @return
	 * @return: JSONObject
	 * @author: 查恒 2017年4月28日 下午4:11:41 
	 */
	@ResponseBody
	@RequestMapping(value = "/productCountChart")
	public JSONObject getProductCountChart() {
		JSONObject json = new JSONObject();
		List<Map<String, Object>> productCountChartList = shopMapper.getProductCountChart();
		String jsonString = JSON.toJSONString(productCountChartList, CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		return json;
	}
	
	

	/** 
	 * @Title: getProductTotalChart 
	 * @Description: 各品类商品订单金额占比
	 * @return
	 * @return: JSONObject
	 * @author: 查恒 2017年4月28日 下午4:12:30 
	 */
	@ResponseBody
	@RequestMapping(value = "/productTotalChart")
	public JSONObject getProductTotalChart() {
		JSONObject json = new JSONObject();
		List<Map<String, Object>> productTotalChartList = shopMapper.getProductTotalChart();
		String jsonString = JSON.toJSONString(productTotalChartList, CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		return json;
	}
	
	
	
	/*****排名列表*****/
	/** 
	 * @Title: viewShopNumTable 
	 * @Description: 当前商品浏览量排名前十的商品
	 * @return
	 * @return: JSONObject
	 * @author: 查恒 2017年5月2日 上午10:18:06 
	 */
	@ResponseBody
	@RequestMapping(value = "/viewShopNumTable")
	public JSONObject viewShopNumTop(){
		JSONObject json = new JSONObject();
		List<Map<String, Object>> shopViewNumList=shopMapper.getShopViewNumTop();
		String jsonString = JSON.toJSONString(shopViewNumList, CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		return json;
	}
	
	
	/** 
	 * @Title: ShopPublishNumTop 
	 * @Description: 当前供应商发布商品数量排名前十
	 * @return
	 * @return: JSONObject
	 * @author: 查恒 2017年5月2日 上午10:18:28 
	 */
	
	@ResponseBody
	@RequestMapping(value = "/shopPublishNumTable")
	public JSONObject ShopPublishNumTop(){
		JSONObject json = new JSONObject();
		List<Map<String, Object>> shopPublishNumList=shopMapper.getShopPublishNumTop();
		String jsonString = JSON.toJSONString(shopPublishNumList, CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		return json;
	}
	
	/** 
	 * @Title: ShopTradeNumTop 
	 * @Description: 当前供应商商品订单交易金额排名前十
	 * @return
	 * @return: JSONObject
	 * @author: 查恒 2017年5月2日 上午10:21:27 
	 */
	@ResponseBody
	@RequestMapping(value = "/shopTradeTotalTable")
	public JSONObject ShopTradeTotalTop(){
		JSONObject json = new JSONObject();
		List<Map<String, Object>> shopTradeNumList=shopMapper.getShopTradeTotalTopTop();
		String jsonString = JSON.toJSONString(shopTradeNumList, CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		return json;
	}
	
	
	/** 
	 * @Title: ShopOrderTotalTop 
	 * @Description: 当前订单（金额）排名前十的品牌
	 * @return
	 * @return: JSONObject
	 * @author: 查恒 2017年5月2日 上午10:23:30 
	 */
	@ResponseBody
	@RequestMapping(value = "/shopOrderBrandTotalTable")
	public JSONObject ShopOrderBrandTotalTop(){
		JSONObject json = new JSONObject();
		List<Map<String, Object>> shopOrderBrandTotalList=shopMapper.getShopOrderBrandTotalTop();
		String jsonString = JSON.toJSONString(shopOrderBrandTotalList, CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		return json;
	}
	
	/** 
	 * @Title: ShopOrderCountTop 
	 * @Description: 当前订单（数量）排名前十的商品
	 * @return
	 * @return: JSONObject
	 * @author: 查恒 2017年5月2日 上午10:24:10 
	 */
	@ResponseBody
	@RequestMapping(value = "/shopOrderCountTable")
	public JSONObject ShopOrderCountTop(){
		JSONObject json = new JSONObject();
		List<Map<String, Object>> shopOrderCountList=shopMapper.getShopOrderCountTop();
		String jsonString = JSON.toJSONString(shopOrderCountList, CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		return json;
	}
	
	/** 
	 * @Title: ShopOrderTotalTop 
	 * @Description: 当前订单（金额）排名前十的商品
	 * @return
	 * @return: JSONObject
	 * @author: 查恒 2017年5月2日 上午10:25:05 
	 */
	@ResponseBody
	@RequestMapping(value = "/shopOrderTotalTable")
	public JSONObject ShopOrderTotalTop(){
		JSONObject json = new JSONObject();
		List<Map<String, Object>> shopOrderTotalList=shopMapper.getShopOrderTotalTop();
		String jsonString = JSON.toJSONString(shopOrderTotalList, CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		return json;
	}
	
	
	//导出Excel
	@RequestMapping("/exportShopDetailExcel")
	public void exportTradeDetailData(Integer type, String startDate, String endDate, HttpServletRequest request,
			HttpServletResponse response) {
		Workbook exportExcel = null;
		List<Map<String, Object>> selectList = null;
		String title = ".xls";
		if (type.equals(0)) {
			// 每月自营商品订单金额及增长率 
			selectList = shopMapper.getProductTotalRate(startDate, endDate);
			exportExcel = ExcelExportUtil.exportExcel(
					new ExportParams(ExcelExportEntityUtil.SHOP_VIEW_TITLE, "自营商品订单金额及增长率", "自营商品订单金额及增长率"),
					ExcelExportEntityUtil.ProductTotalRateList(), selectList);
			title = ExcelExportEntityUtil.SHOP_VIEW_TITLE + "自营商品订单金额及增长率" + title;
		} else if (type.equals(1)) {
			// 每月商品订单金额及增长率
			selectList = shopMapper.getShopTotalRate(startDate, endDate);
			exportExcel = ExcelExportUtil.exportExcel(
					new ExportParams(ExcelExportEntityUtil.SHOP_VIEW_TITLE, "订单金额及增长率", "订单金额及增长率"),
					ExcelExportEntityUtil.ShopTotalRateList(), selectList);
			title = ExcelExportEntityUtil.SHOP_VIEW_TITLE + "订单金额及增长率" + title;
		} else if (type.equals(2)) {
			// 每月发布商品数量及增长率
			selectList = shopMapper.getProductCountRate(startDate, endDate);
			exportExcel = ExcelExportUtil.exportExcel(
					new ExportParams(ExcelExportEntityUtil.SHOP_VIEW_TITLE, "发布商品数量及增长率", "发布商品数量及增长率"),
					ExcelExportEntityUtil.ProductCountRateList(), selectList);
			title = ExcelExportEntityUtil.SHOP_VIEW_TITLE + "发布商品数量及增长率" + title;
		}

		try {
			response.setContentType("application/vnd.ms-excel;");
			response.setHeader("Content-Disposition", "attachment;filename=" + CommonUtil.toUtf8String(request.getHeader("User-Agent") , title));
			ServletOutputStream outputStream = response.getOutputStream();
			exportExcel.write(outputStream);
			outputStream.flush();
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	
	/**店铺概览结束*/

	
	/***店铺明细开始***/
	@MenuPermission
	@RequestMapping(value = "/shopdetail")
	public String shopdetail(Integer type, ModelMap model) {
		if (type == null) {
			type = 0;
		}
		model.addAttribute("type", type);
		return "shop/shop_list";
	}

	@ResponseBody
	@RequestMapping(value = "/shopdetaildata")
	public JSONObject shopdetaildata(HttpServletRequest request, Integer pageNo,  Integer type,
			ModelMap model) {
		// 搜索条件
		Map searchMap = RequestUtils.getSearchMap(request);
		JSONObject json = new JSONObject();
		List<Map<String, Object>> selectList = null;
		Page<Object> startPage = null;
		// 列表序号
		if (type == null) {
			type = 0;
		}
		if (type.equals(1)) {
			//所有店铺供应商列表
			startPage = PageHelper.startPage(pageNo == null ? 1 : pageNo, Globals.PAGE_SIZE);
			selectList = shopMapper.selectAllShop(searchMap);
		}else {
			//所有商品列表
			startPage = PageHelper.startPage(pageNo == null ? 1 : pageNo, Globals.PAGE_SIZE);
			selectList = shopMapper.selectProductList(searchMap);
		}

		Pagination paginationList = paginationService.getPaginationList(startPage, selectList);
		RequestUtils.MapToModelMap(searchMap, request);
		String jsonString = JSON.toJSONString(paginationList.getList(), CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		json.put("draw", paginationList.getPageNo());
		json.put("total", paginationList.getTotalCount());
		json.put("recordsTotal", paginationList.getTotalCount());
		json.put("recordsFiltered", paginationList.getTotalCount());
		return json;
	}

	/*
	 商品浏览记录
	 */
	@MenuPermission
	@RequestMapping(value = "/productVisit")
	public String productVisit(Integer type, ModelMap model , Integer so_proId) {
		if (type == null) {
			type = 0;
		}
		model.addAttribute("type", type);
		model.addAttribute("so_proId", so_proId);
		return "shop/product_visit";
	}


	@ResponseBody
	@RequestMapping(value = "/productVisitDetail")
	public JSONObject productVisitDetail(HttpServletRequest request, Integer pageNo,  Integer type ) {
		// 搜索条件
		Map searchMap = RequestUtils.getSearchMap(request);
		JSONObject json = new JSONObject();
		List<Map<String, Object>> selectList = null;
		Page<Object> startPage = null;
		// 列表序号
		if (type == null) {
			type = 0;
		}
		if (type.compareTo(0) == 0) {
			//商品浏览记录列表
			startPage = PageHelper.startPage(pageNo == null ? 1 : pageNo, Globals.PAGE_SIZE);
			selectList = shopMapper.selectProductVisitData(searchMap);
		}else if(type.compareTo(1) == 0){
			//商品浏览详情
			startPage = PageHelper.startPage(pageNo == null ? 1 : pageNo, Globals.PAGE_SIZE);
			selectList = shopMapper.productVisitDetail(searchMap);
		}

		Pagination paginationList = paginationService.getPaginationList(startPage, selectList);
		RequestUtils.MapToModelMap(searchMap, request);
		String jsonString = JSON.toJSONString(paginationList.getList(), CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		json.put("draw", paginationList.getPageNo());
		json.put("total", paginationList.getTotalCount());
		json.put("recordsTotal", paginationList.getTotalCount());
		json.put("recordsFiltered", paginationList.getTotalCount());
		return json;
	}



	
	//导出Excel
	@ResponseBody
	@RequestMapping(value = "/exportShopListData")
	public void exportShopListData(Integer type, HttpServletRequest request, HttpServletResponse response) {
		Workbook exportExcel = null;
		Map searchMap = RequestUtils.getSearchMap(request);
		List<Map<String, Object>> selectList = null;
		String title = ".xls";
		if (type.equals(0)) {
			// 所有商品列表
			selectList = shopMapper.selectProductList(searchMap);
			exportExcel = ExcelExportUtil.exportExcel(
					new ExportParams(ExcelExportEntityUtil.SHOP_DETAIL_TITLE, "商品列表", "商品列表"),
					ExcelExportEntityUtil.ProductList(), selectList);
			title = ExcelExportEntityUtil.SHOP_DETAIL_TITLE + "商品列表" + title;
		} else if (type.equals(1)) {
			// 所有店铺供应商列表
			selectList = shopMapper.selectAllShop(searchMap);
			exportExcel = ExcelExportUtil.exportExcel(
					new ExportParams(ExcelExportEntityUtil.SHOP_DETAIL_TITLE, "店铺列表", "店铺列表"),
					ExcelExportEntityUtil.AllShopList(), selectList);
			title = ExcelExportEntityUtil.SHOP_DETAIL_TITLE + "店铺列表" + title;
		}

		try {
			response.setContentType("application/vnd.ms-excel;");
			response.setHeader("Content-Disposition", "attachment;filename=" + CommonUtil.toUtf8String(request.getHeader("User-Agent") , title));
			ServletOutputStream outputStream = response.getOutputStream();
			exportExcel.write(outputStream);
			outputStream.flush();
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 导出商品浏览的数据
	 * @param type
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@RequestMapping(value = "/exportProductVisitData")
	public void exportProductVisitData(Integer type, HttpServletRequest request, HttpServletResponse response) {
		Workbook exportExcel = null;
		Map searchMap = RequestUtils.getSearchMap(request);
		List<Map<String, Object>> selectList = null;
		String title = ".xls";
		if (type.equals(0)) {
			// 所有商品列表
			selectList = shopMapper.selectProductVisitData(searchMap);
			exportExcel = ExcelExportUtil.exportExcel(
					new ExportParams("商品热度列表", "商品热度列表"),
					ExcelExportEntityUtil.ProductVisitList(), selectList);
			title = "商品热度列表" + title;
		} else if (type.equals(1)) {
			// 所有店铺供应商列表
			selectList = shopMapper.productVisitDetail(searchMap);
			exportExcel = ExcelExportUtil.exportExcel(
					new ExportParams("商品访问记录", "商品访问记录"),
					ExcelExportEntityUtil.ProductVisitDataList(), selectList);
			title = "商品访问记录" + title;
		}

		try {
			response.setContentType("application/vnd.ms-excel;");
			response.setHeader("Content-Disposition", "attachment;filename=" + CommonUtil.toUtf8String(request.getHeader("User-Agent") , title));
			ServletOutputStream outputStream = response.getOutputStream();
			exportExcel.write(outputStream);
			outputStream.flush();
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	
	/***店铺明细结束***/
	
	
}
